package com.z.playbutton;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 一个支持动画额播放暂停按钮.
 * <p>
 * Created by z
 * on 2017/11/26 0026.
 */

public class PlayButton extends View {

    private int mWidth;     //控件宽度
    private int mHeight;    //控件高度
    private int mDiameter;  //控件直径,去宽高之中的最小值

    private Path mLeftPath;     //左边竖线的path
    private Path mRightPath;    //右边竖线或者折线的path
    private Paint mPaint;       //画笔

    private final int DEFAULT_DIAMETER = 100;   //默认半径50px

    public PlayButton(Context context) {
        super(context);
        init();
    }

    public PlayButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PlayButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        switch (MeasureSpec.getMode(widthMeasureSpec)) {
            case MeasureSpec.EXACTLY:
                mWidth = MeasureSpec.getSize(widthMeasureSpec);
                break;
            case MeasureSpec.AT_MOST:
                mWidth = DEFAULT_DIAMETER;
                break;
            case MeasureSpec.UNSPECIFIED:
                mWidth = DEFAULT_DIAMETER;
                break;
        }
        switch (MeasureSpec.getMode(heightMeasureSpec)) {
            case MeasureSpec.EXACTLY:
                mHeight = MeasureSpec.getSize(heightMeasureSpec);
                break;
            case MeasureSpec.AT_MOST:
                mHeight = DEFAULT_DIAMETER;
                break;
            case MeasureSpec.UNSPECIFIED:
                mHeight = DEFAULT_DIAMETER;
                break;
        }
        mDiameter = mWidth < mHeight ? mWidth : mHeight;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);
        mPaint.setColor(0xFF888888);            //设置颜色
        mPaint.setStrokeWidth(mDiameter / 25);               //设置线宽
        mPaint.setStyle(Paint.Style.STROKE);    //设置描边
        mPaint.setStrokeCap(Paint.Cap.ROUND);   //设置末端圆角
        mPaint.setStrokeJoin(Paint.Join.ROUND); //设置交汇圆角
        mPaint.setAntiAlias(true);  //设置抗锯齿
        mPaint.setDither(true);     //设置抖动
        calculatePath(1);
        canvas.drawPath(mLeftPath, mPaint);
        canvas.drawPath(mRightPath, mPaint);

    }

    private void init() {
        mPaint = new Paint();
        mLeftPath = new Path();
        mRightPath = new Path();
    }

    /**
     * 根据进度计算Path.
     *
     * @param progress 从0到1变化.0是暂停按钮(||),1是播放按钮(|>)
     */
    private void calculatePath(float progress) {
        float space;    //必要的间距,为了让控件是圆形时也可以放下内容
        space = mDiameter * 0.3f;
        float offsetX = (mWidth - mDiameter) / 2.0f;
        float offsetY = (mHeight - mDiameter) / 2.0f;
        //左边的竖线始终不变,一堆谜一样的计算只是为了当width != height时能将图形显示在中心位置
        mLeftPath.reset();
        mLeftPath.moveTo(space + offsetX, space + offsetY);
        mLeftPath.lineTo(space + offsetX, mHeight - (space + offsetY));
        //右边的竖线根据按钮状态可能会是折线,一堆谜一样的计算只是为了当width != height时能将图形显示在中心位置
        mRightPath.reset();
        //最终x随着进度的坐标值
        float progressOffsetX = progress * (mWidth - 2 * (space + offsetX));
        mRightPath.moveTo(mWidth - (space + offsetX) - progressOffsetX, space + offsetY);
        mRightPath.lineTo(mWidth - (space + offsetX), mHeight / 2);
        mRightPath.lineTo(mWidth - (space + offsetX) - progressOffsetX, mHeight - (space + offsetY));


    }


}
